<template>
    <div>
      <div class="top-panel">
        <el-card>
          <el-form :model="searchForm" label-width="100px" label-position="right">
            <el-row>
              <el-col :span="5">
                <el-form-item label="设备名称">
                  <el-input
                    class="password-input"
                    v-model="searchForm.deviceNameFuzzy"
                    clearable
                    placeholder="支持模糊搜索"
                    @keyup.enter="loadDataList"
                  >
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="5">
                <el-form-item label="申请日期" >
                  <el-date-picker v-model="searchForm.joinTimeRange" type="daterange" range-separator="~"
                    start-placeholder="开始日期" end-placeholder="结束日期" value-format="YYYY-MM-DD" @change="loadDataList">
                  </el-date-picker>
                </el-form-item>
              </el-col>

              <el-col :span="4" :style="{ paddingLeft: '10px' }">
                <el-button type="success" @click="loadDataList()"
                  >查询
              </el-button>
              </el-col>
            </el-row>
          </el-form>
        </el-card>
      </div>
      <el-card class="table-data-card">
        <template #header>
          <span>设备申请列表</span>
        </template>
        <Table
          ref="tableInfoRef"
          :columns="columns"
          :fetch="loadDataList"
          :dataSource="tableData"
          :options="tableOptions"
          :extHeight="tableOptions.extHeight"
        >
          <template #slotStatus="{ index, row }">
            <div class="row-op-panel">
              <Badge showType="red" text="已取消" v-if="row.status == 5"></Badge>
              <Badge showType="green" text="逾期未还" v-if="row.status == 4"></Badge>
              <Badge showType="green" text="已归还" v-if="row.status == 3"></Badge>
              <Badge showType="green" text="已拒绝" v-if="row.status == 2"></Badge>
              <Badge showType="green" text="已通过" v-if="row.status == 1"></Badge>
              <Badge showType="red" text="待审批" v-if="row.status == 0"></Badge>
            </div>
          </template>


          <template #slotOperation="{ index, row }">
            <div
              class="row-op-panel"
            >
              <a
                @click="updateStatus(row,5)"
                class="a-link"
                v-if="row.status==0"
                href="javascript:void(0)"
                >取消</a
              >
              <a
                class="a-link"
                style="color:rgb(170, 176, 182)"
                v-else
                href="javascript:void(0)"
                >取消</a
              >

            </div>
          </template>
        </Table>
      </el-card>
      <DeviceEdit ref="deviceEditRef" @reload="loadDataList"></DeviceEdit>
    </div>
  </template>

  <script setup>
  import DeviceEdit from "./ApplyEdit.vue"
  import { getCurrentInstance, nextTick, ref } from "vue";
  const { proxy } = getCurrentInstance();
  const userInfo = ref(proxy.VueCookies.get("userInfo") || {});

  const api = {
    loadDataList: "/deviceApply/loadApplyList4User",
    updateStatus: "/deviceApply/updateStatus",
  };

  const tableData = ref({});
  const tableOptions = ref({
    extHeight: 125,
  });

  const columns = [
    {
      label: "申请编号",
      prop: "deviceApplyId",
    },
    {
      label: "承诺归还时间",
      prop: "promiseTime",
    },
    {
      label: "申请时间",
      prop: "applyTime",
    },
    {
      label: "管理授权时间",
      prop: "grantTime",
    },
    {
      label: "归还时间",
      prop: "returnTime",
    },
    {
      label: "完成时间",
      prop: "finishTime",

    },
    {
      label: "状态",
      scopedSlots: "slotStatus",
    },
    {
      label: "设备编号",
      prop: "deviceId",
    },
    {
      label: "设备名称",
      prop: "deviceName",
    },
    {
      label: "操作",
      prop: "operation",
      scopedSlots: "slotOperation",
    },
  ];


  const tableInfoRef = ref();
  const searchForm = ref({});
  const loadDataList = async () => {
    let params = {
      pageNo: tableData.value.pageNo,
      pageSize: tableData.value.pageSize,
    };
    Object.assign(params, searchForm.value);
    if (searchForm.value.joinTimeRange) {
      params.applyTimeStart = searchForm.value.joinTimeRange[0]
      params.applyTimeEnd = searchForm.value.joinTimeRange[1]
    }
    delete params.joinTimeRange
    let result = await proxy.Request({
      url: api.loadDataList,
      params: params,
    });
    if (!result) {
      return;
    }
    Object.assign(tableData.value, result.data);
  };

  //删除
  const updateStatus = (data,status) => {
    proxy.Confirm(`确定要执行操作吗？`, async () => {
      let result = await proxy.Request({
        url: api.updateStatus,
        params: {
          applyId: data.deviceApplyId,
          status:status,
        },
      });
      if (!result) {
        return;
      }
      proxy.Message.success("操作成功");
      loadDataList();
    });
  };


  const bookEditRef = ref();
  const showEdit = (data = {}) => {
    bookEditRef.value.showEdit(Object.assign({}, data));
  };


  </script>

  <style lang="scss" scoped>
  .detail-tree-panel {
    height: calc(100vh - 273px);
    overflow: auto;
    width: 100%;
  }
  </style>
